<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>B站评论回车发布</title>
    <style>
        .wrapper {
            min-width: 400px;
            max-width: 800px;
            display: flex;
            justify-content: flex-end;
        }

        .avatar {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            overflow: hidden;
            background: url(./images/avatar.jpg) no-repeat center / cover;
            margin-right: 20px;
        }

        .wrapper textarea {
            outline: none;
            border-color: transparent;
            resize: none;
            background: #f5f5f5;
            border-radius: 4px;
            flex: 1;
            padding: 10px;
            transition: all 0.5s;
            height: 30px;
        }

        .wrapper textarea:focus {
            border-color: #e4e4e4;
            background: #fff;
            height: 50px;
        }

        .wrapper button {
            background: #00aeec;
            color: #fff;
            border: none;
            border-radius: 4px;
            margin-left: 10px;
            width: 70px;
            cursor: pointer;
        }

        .wrapper .total {
            margin-right: 80px;
            color: #999;
            margin-top: 5px;
            opacity: 0;
            transition: all 0.5s;
        }

        .list {
            min-width: 400px;
            max-width: 800px;
            display: flex;
        }

        .list .item {
            width: 100%;
            display: flex;
        }

        .list .item .info {
            flex: 1;
            border-bottom: 1px dashed #e4e4e4;
            padding-bottom: 10px;
        }

        .list .item p {
            margin: 0;
            /* 单行文本超出...*/
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .list .item .name {
            color: #FB7299;
            font-size: 14px;
            font-weight: bold;
        }

        .list .item .text {
            color: #333;
            padding: 10px 0;
        }

        .list .item .time {
            color: #999;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <i class="avatar"></i>
        <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
        <button>发布</button>
    </div>
    <div class="wrapper">
        <span class="total">0/200字</span>
    </div>
    <div class="list">
        <div class="item" style="display: none;">
            <i class="avatar"></i>
            <div class="info">
                <p class="name">清风徐来</p>
                <p class="text">大家都辛苦啦，感谢各位大大的努力，能圆满完成真是太好了[笑哭][支持]</p>
                <p class="time">2022-10-10 20:29:21</p>
            </div>
        </div>
    </div>

    <script>
        // 获取对应dom元素标签
        const total = document.querySelector('.total');
        const tx = document.querySelector('#tx');
        // 留言结果标签
        const text = document.querySelector('.text');
        const item = document.querySelector('.item');
        const button = document.querySelector('button');
        const time = document.querySelector('.time');
        let y, m, d, h, ms, s;

        // 注册绑定鼠标获得焦点事件,显示字数统计
        tx.addEventListener('focus', function (e) {
            total.style.opacity = 1;
        });
        // 注册绑定鼠标失去焦点事件，隐藏字数统计
        tx.addEventListener('blur', function (e) {
            total.style.opacity = 0;
        });

        // 用户输入事件事件，实时统计用户输入的字数
        tx.addEventListener('input', function (e) {
            total.innerHTML = `${tx.value.length}/200字`;
        });

        // 注册绑定点击发布按钮事件，点击后将数据传入对应的标签
        button.addEventListener('click', function (e) {
            // 调用判断用户输入的数据是否为空函数
            getData();
            getUserData();
        });

        // 注册绑定键盘回车事件，将输入传入独赢标签
        tx.addEventListener('keyup', function (e) {
            // 判断用户按下的是否是回车键
            if (e.key === 'Enter') {
                // 调用判断用户输入的数据是否为空函数
                getData();
                getUserData();
            };
        });

        // 封装用户输入的数据传值方法
        function getUserText() {
            text.innerHTML = tx.value.trim();
            item.style.display = 'block';
            tx.value = '';
            total.innerHTML = '0/200字';
            time.innerHTML = `${years}-${month}-${day} ${hour}:${minutes}:${seconds}`;
        };

        // 封装判断用户输入内容是否为空函数
        function getUserData() {
            // 判断用户输入的数据是否为空数据
            if (tx.value.trim()) {
                getUserText();
            }
            tx.value = '';
            total.innerHTML = '0/200字';
        }

        // 获取当前时间函数
        function getData() {
            // 获取当前时间
            let data = new Date();
            years = data.getFullYear();
            month = data.getMonth();
            day = data.getDate();
            hour = data.getHours();
            minutes = data.getMinutes();
            seconds = data.getSeconds();
            // 三元运算符对月份、天数、分钟、秒补零操作
            month = month < 10 ? '0' + (month + 1) : month + 1;
            day = day < 10 ? '0' + day : day;
            minutes = minutes < 10 ? '0' + minutes : minutes;
            seconds = seconds < 10 ? '0' + seconds : seconds;
        }
    </script>
</body>

</html>